package ru.cybertek.struts2.action;

import com.opensymphony.xwork2.ActionSupport;

import ru.cybertek.dao.AccountDao;
import ru.cybertek.domain.Account;
import ru.cybertek.domain.Person;
import ru.cybertek.domain.Company;
import ru.cybertek.dao.BankDao;
import ru.cybertek.dao.PersonDao;
import ru.cybertek.dao.CompanyDao;
import ru.cybertek.domain.constant.AccountType;

public class AccountManagementAction extends ActionSupport  
{
	private String balance;
	
	private String accountNumber;
	
	private AccountDao accountDao;
	
	private String id;
	
	private int isCompany = 1;
	
	private BankDao bankDao;
	
	private int bankId;
	
	private CompanyDao companyDao;
	
	private PersonDao personDao;

	private int personId;
	
	private int companyId;
	

	/**
	 * List of accounts
	 * 
	 * @return SUCCESS
	 */
	public String list()
	{
		return SUCCESS;
	}
	
	/**
	 * Add/modify account
	 * 
	 * @return SUCCESS
	 */
	public String add()
	{
		Account account = new Account();
		account.setBalance(Double.valueOf(balance));
		account.setAccountNumber(accountNumber);
		
		account.setBank(bankId);
		
		if ((id != null) & !id.equals(""))
			account.setId(Integer.valueOf(id));
		
		if (Integer.valueOf(isCompany) == 1)
		{
			Company company = new Company(companyId);
			account.setOwner(company);
		}
		else
		{
			Person person = new Person(personId);
			account.setOwner(person);
		}
		
		accountDao.saveAccount(account);
		
		clearFieldValue();
		
		return SUCCESS;
	}
	
	/**
	 * Get account information for edit purpose
	 * 
	 * @return SUCCESS
	 */
	public String edit()
	{
		Account account = accountDao.getAccount(Integer.valueOf(id));
		
		balance = account.getBalance().toString();
		accountNumber = account.getAccountNumber();
		id = account.getId().toString();
		isCompany = account.getAccountType().getTypeCode();
		bankId = account.getBank().getId();
		if (account.getAccountType() == AccountType.CORPORATE)
		{
			companyId = account.getOwner().getId();
			personId = 0;
		}
		else
		{
			personId = account.getOwner().getId();
			companyId = 0;
		}
		
		return SUCCESS;
	}

	/**
	 * Delete account
	 * 
	 * @return SUCCESS
	 */
	public String delete()
	{
		accountDao.deleteAccount(Integer.valueOf(id));
		
		clearFieldValue();
		
		return SUCCESS;
	}
	
	/**
	 * Cancel action 
	 * 
	 * @return SUCCESS
	 */
	public String cancel()
	{
		clearFieldValue();
		
		return SUCCESS;
	}
	
	/**
	 * Clear page field values
	 */
	private void clearFieldValue()
	{
		id = null;
		balance = null;
		accountNumber = null;
		isCompany = 1;
		bankId = 0;
		personId = 0;
		companyId = 0;
	}

	
	
	
	public String getBalance() {
		return balance;
	}

	public void setBalance(String balance) {
		this.balance = balance;
	}

	public String getAccountNumber() {
		return accountNumber;
	}

	public void setAccountNumber(String accountNumber) {
		this.accountNumber = accountNumber;
	}

	public AccountDao getAccountDao() {
		return accountDao;
	}

	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	public int getIsCompany()
	{
		return isCompany;
	}

	public void setIsCompany(int isCompany)
	{
		
		this.isCompany = isCompany;
	}
	
	public void setBankDao(BankDao bankDao)
	{
		this.bankDao = bankDao;
	}
	
	public BankDao getBankDao()
	{
		return bankDao;
	}
	
	public void setBankId(int bankId) 
	{
		this.bankId = bankId;
	}

	public int getBankId()
	{
		return bankId;
	}

	public CompanyDao getCompanyDao() {
		return companyDao;
	}

	public void setCompanyDao(CompanyDao companyDao) {
		this.companyDao = companyDao;
	}

	public int getPersonId() {
		return personId;
	}

	public void setPersonId(int personId) {
		this.personId = personId;
	}

	public int getCompanyId() {
		return companyId;
	}

	public void setCompanyId(int companyId) {
		this.companyId = companyId;
	}

	public PersonDao getPersonDao() {
		return personDao;
	}

	public void setPersonDao(PersonDao personDao) {
		this.personDao = personDao;
	}

}

